<template>
  <el-dialog
    v-dialogDrag
    :title="title"
    modal
    class="pms-dialog"
    v-bind="$attrs"
    width="60%"
    v-on="$listeners"
  >
    <div class="container">
      <div class="select">
        <div class="box">
          <div class="title">可选办理人</div>
          <div class="content">
            <el-tree
              ref="userTree"
              class="userTree-container"
              empty-text=""
              :data="toSelectdata"
              default-expand-all
              icon-class=""
              @current-change="currentChange"
              @node-click="userClick"
            >
              <template slot-scope="{ node, data }">
                <span class="custom-tree-node">
                  <span v-if="data.isLeaf == '0'" style="margin-right: 6px;font-size: 14px;color: #606266;" :class="[node.expanded ? 'el-icon-folder-opened' : 'el-icon-folder']" />
                  <span v-else style="margin-right: 6px;font-size: 14px;color: #606266;" class="el-icon-user" />
                  <span style="font-size: 14px;color: #606266;">{{ data.text }}</span>
                </span>
              </template>
            </el-tree>
          </div>
        </div>
        <div class="box">
          <div class="title">已选办理人</div>
          <div class="content">
            <div v-if="userSelectedName" class="user-select-item">
              <span class="el-icon-user" style="margin-right: 6px;" />
              <span>{{ userSelectedName }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div slot="footer">
      <el-button class="pms-button-primary" @click="confirm">确 定</el-button>
      <el-button class="pms-button-primary" @click="cancel">取 消</el-button>
    </div>
  </el-dialog>

</template>

  <script>
  import { getSelectPersonForTransferTask } from '@/api/SettlementAndFinalAccounts/projectAuthorizationMigration/index' // 列表接口

  export default {
    name: 'AlterationPage',
    props: {
      title: {
        type: String,
        default: '变更办理人'
      },
      alterationData: {
        type: [Array, String],
        default: ''
      }
    },
    data() {
      return {
        userSelected: {}, // 已选择的人员节点
        toSelectdata: [], // 可选办理人树型
        selectedUserId: '' // 已选办理人账号
      }
    },
    computed: {
      userSelectedName() {
        return this.userSelected.empName
      }
    },
    watch: {
    },
    mounted() {
        this.doSearchNextUsersByRole()
    },
    methods: {
      // 获取节点可选办理人
      doSearchNextUsersByRole() {
        const param = {
            'prjId': this.alterationData[0].PRJID,
            'staffId': this.alterationData[0].GOTOSHMANAGER
        }
        getSelectPersonForTransferTask(param).then(res => {
          if (+res.code !== 200) {
            return Promise.reject(res)
          }
          this.toSelectdata = res.data
        })
      },
      currentChange(data, node) {
        console.log('123123')
      },
      userClick(data, node) {
        if (data.pid && node.expanded) {
          const { id: empId, text: empName } = data
          this.userSelected = { empId, empName }
          this.selectedUserId = data.id
        }
      },
      // 提交后返回值
      doEmit() {
        this.$emit('changeSave', {
          ...this.userSelected
        })
        this.$emit('update:visible', false)
      },
      // 确认
      confirm() {
        if (!Object.keys(this.userSelected).length) {
          this.$message({
            message: '请选择办理人',
            type: 'error'
          })
          return
        }
        this.doEmit()
      },
      // 取消
      cancel() {
          this.$emit('selected', {})
          this.$emit('update:visible', false)
      }
    }
  }
  </script>
  <style lang='scss' scoped>
  ::v-deep .el-dialog__body {
      padding: 20px;
  }
    .container {
      .select {
        display: flex;
        align-items: center;
        .box {
          width: 100%;
          &:nth-child(n+2){
            margin-left: 10px;
          }
        }
        .title {
          width: 100%;
          height: 36px;
          line-height: 36px;
          text-align: left;
          font-size: 17px;
          font-weight: 900;
          // color: rgb(227, 47, 47);
          color: #eb4b4b;
        }
        .content {
          height: 250px;
          border: 1px solid #ccc;
          overflow: hidden;
          .node-item {
            height: 30px;
            line-height: 30px;
          //   text-align: left;
            text-indent: 1em;
            font-size: 12px;
            width: 100%;
            background-color: #DBDBDB;
            &:hover {
              cursor: pointer;
            }
            &.clicked {
              background-color: #ffefe5;
              color: #eb4b4b;
            }
          }
          .user-item {
            height: 22px;
            line-height: 22px;
          }
          .user-select-item {
            height: 30px;
            line-height: 30px;
            text-align: center;
            font-size: 12px;
            width: 100%;
            background-color: #F4F6F8;
          }
        }
        .icon {
          margin-right: 6px;
          font-size: 14px;
        }
        ::v-deep .el-icon-caret-right:before {
          content: ''
        }
      }
      .message {
        //margin-top: 10px;
        text-align: left;
      }
      .nextTips{
        margin-top: 5px;
        text-align: left;
      }
    }
    .userTree-container{
      height: 250px;
      overflow-y: auto;
    }
    .nextPageTittle{
      text-align: left;
      line-height: 24px;
      font-size: 18px;
      color: #303133;
    }
  .el-button--text {
      // color: #02A7F0;
      color: #1b3f91;
      padding: 0;
      margin-bottom: 10px;
      line-height: 16px;
  }
  ::v-deep .el-form-item__error {
      line-height: 0;
      top: 120px;
      left: -60px;
  }
  </style>
